<template>
  <div class="product-list">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="商品分类">
          <el-select
            size="small"
            v-model="searchForm.categoryId"
            placeholder="所有分类"
          >
            <el-option label="全部" value="0"></el-option>
            <el-option
              v-for="(item, index) in categoryList"
              :key="index"
              :label="item.name"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属商户">
          <el-select
            size="small"
            v-model="searchForm.supplierId"
            filterable
            placeholder="所属商户"
          >
            <el-option label="全部" value="0"></el-option>
            <el-option
              v-for="(item, index) in supplierList"
              :key="index"
              :label="item.name"
              :value="item.shopSupplierId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            size="small"
            v-model="searchForm.productName"
            placeholder="请输入商品名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="onSubmit"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="success" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
          ref="multipleTable"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="productName" label="产品" width="300px">
            <template #default="scope">
              <div class="product-info">
                <div class="pic">
                  <img v-img-url="scope.row.imagePath" alt="" />
                </div>
                <div class="info">
                  <div class="name">{{ scope.row.productName }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="supplierName"
            label="所属商户"
          ></el-table-column>
          <el-table-column prop="productPrice" label="价格">
            <template #default="scope">
              <div class="gray3">￥{{ scope.row.productPrice }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="productStock" label="库存"></el-table-column>
          <el-table-column
            prop="salesActual"
            label="实际销量"
          ></el-table-column>
          <el-table-column fixed="right" label="操作" width="80">
            <template #default="scope">
              <div class="table-btn-column">
                <el-button
                  @click="agentClick(scope.row)"
                  type="text"
                  size="small"
                  >详情</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
    <!--添加-->
    <Agent
      v-if="open_agent"
      :open_agent="open_agent"
      :productModel="productModel"
      @closeDialog="closeDialogFunc($event, 'Agent')"
    ></Agent>
  </div>
</template>

<script>
import AgentPorductApi from "@/api/plus/agent/product.js";
import Agent from "./dialog/Agent.vue";
export default {
  components: {
    Agent,
  },
  data() {
    return {
      /*切换选中值*/
      activeIndex: "0",
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*搜索参数*/
      searchForm: {
        productName: "",
        categoryId: "",
      },
      /*列表数据*/
      tableData: [],
      /*全部分类*/
      categoryList: [],
      supplierList: [],
      /*商品统计*/
      product_count: {},
      open_agent: false,
      productModel: {},
      selectAll: false,
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },

    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },

    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      self.loading = true;
      AgentPorductApi.getList(Params, true)
        .then((res) => {
          self.loading = false;
          self.selectAll = false;
          self.tableData = res.data.productList.records;
          self.categoryList = res.data.categoryList;
          self.supplierList = res.data.supplierList;
          self.totalDataNumber = res.data.productList.total;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    reset() {
      this.curPage = 1;
      this.searchForm = {
        product_name: "",
        category_id: "",
      };
      this.getData();
    },
    agentClick(row) {
      this.productModel = {
        productId: row.productId,
        productName: row.productName,
        isAgent: row.isAgent,
        isIndAgent: row.isIndAgent,
      };
      this.open_agent = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "Agent") {
        this.open_agent = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    },
    allSelection(rows) {
      this.$refs.multipleTable.toggleAllSelection();
    },
    changeAgent(type) {
      let self = this;
      let list = this.$refs.multipleTable.selection;
      if (list.length <= 0) {
        return;
      }
      let params = [];
      list.forEach((item, index) => {
        params.push(item.productId);
      });
      AgentPorductApi.setAgent(
        {
          productIds: params.join(","),
          isAgent: type,
        },
        true
      )
        .then((res) => {
          self.loading = false;
          ElMessage({
            message: res.msg,
            type: "success",
          });
          self.getData();
        })
        .catch((error) => {
          self.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
